package com.atguigu.gulimail.cart.controller;

import com.atguigu.gulimail.cart.interceptor.CartInterceptor;
import com.atguigu.gulimail.cart.service.CartService;
import com.atguigu.gulimail.cart.vo.Cart;
import com.atguigu.gulimail.cart.vo.CartItem;
import com.atguigu.gulimail.cart.vo.UserInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {

    @Autowired
    CartService cartService;

    /**
     * 购物车列表页面
     *  分为： 1.游客购物车    2.已登录用户购物车
     *  在跳转之前需要通过拦截器前置处理，获取用户信息，并保存到ThreadLocal中
     *  在此获取ThreadLocal中的数据
     * @return
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model){

        UserInfoTo userInfoTo = CartInterceptor.userInfoToThreadLocal.get();
        Cart cart =cartService.getCartList(userInfoTo);
        model.addAttribute("cart",cart);
        return "cartList";
    }

    /**
     * 添加商品到购物车，成功跳转页面
     * 将商品的信息存储到Redis，持久化到数据库中
     * @return
     */
    @GetMapping("/addCartItem")
    public String addCartItem(@RequestParam("skuId") Long skuId,
                          @RequestParam("num")Integer num,
                          RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {

        cartService.addSku(skuId, num);
        redirectAttributes.addAttribute("skuId",skuId);
        return "redirect:http://cart.gulimail.com/addCartItemSuccess.html";
    }


    @GetMapping("/addCartItemSuccess.html")
    public String Success(@RequestParam("skuId") Long skuId,Model model){
        CartItem cartItem=cartService.getCartItem(skuId);
        model.addAttribute(cartItem);
        return "success";
    }

    //选中购物项，修改Check
    @GetMapping("/checkItem")
    public String check(@RequestParam("skuId")Long skuId,@RequestParam("checked")Integer checked){

        cartService.setItemCheck(skuId,checked==0?false:true);
        return "redirect:http://cart.gulimail.com/cart.html";
    }

    //购物车更改数据项数量
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId")Long skuId,@RequestParam("num")Integer num){

        cartService.countItem(skuId,num);

        return "redirect:http://cart.gulimail.com/cart.html";
    }

    //删除购物车数据项
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId")Long skuId){

        cartService.deleteItem(skuId);

        return "redirect:http://cart.gulimail.com/cart.html";
    }

    @GetMapping("currentUserCartItems")
    @ResponseBody
    public List<CartItem> getUserCartItems(){
        UserInfoTo userInfoTo = CartInterceptor.userInfoToThreadLocal.get();
        if(userInfoTo==null){
            return null;
        }else {
            List<CartItem> list=cartService.getUerCartItems();
            return list;
        }

    }

}
